// the brothers problem
// ml:ccf += -pthread
// ml:ldf += -pthread
#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
#include <random>

std::vector<int> counts{100, 200};
std::atomic<int> flag[2]{{}};
std::atomic<int> turn;

int main()
{
    int a{0}, b{0};
    std::vector<std::thread> threads;

    for (size_t i = 0; i < 2; i++)
        threads.emplace_back([count = counts[i], i, &a, &b]() mutable {
            flag[i] = true;
            turn = !i;
            while (flag[!i] && turn == !i) {
            }

            std::random_device rd;
            std::mt19937 gen{rd()};
            std::uniform_int_distribution<> dis{1, 10000};
            while (count--) {
                auto x = dis(gen);
                a += x;
                b -= x;
            }

            flag[i] = false;
        });

    for (auto & thread : threads)
        if (thread.joinable()) thread.join();
    std::cerr << "\e[31m" << a << " + \e[32m" << b << "\e[0m = " << a + b << "\n";
}

